package cn.com.stary1993.kite.captcha.support.behavior;

import cloud.tianai.captcha.generator.ImageCaptchaGeneratorProvider;
import cloud.tianai.captcha.generator.ImageTransform;
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
import cn.hutool.core.util.ArrayUtil;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;

/**
 * 基于spring的 多验证码生成器
 *
 * @author stary1993
 */
public class SpringMultiBehaviorImageCaptchaGenerator extends MultiImageCaptchaGenerator {

	private ListableBeanFactory beanFactory;

	public SpringMultiBehaviorImageCaptchaGenerator(ImageCaptchaResourceManager imageCaptchaResourceManager,
			ImageTransform imageTransform, BeanFactory beanFactory) {
		super(imageCaptchaResourceManager, imageTransform);
		this.beanFactory = (ListableBeanFactory) beanFactory;
	}

	@Override
	protected void doInit(boolean initDefaultResource) {
		super.doInit(initDefaultResource);
		String[] beanNamesForType = beanFactory.getBeanNamesForType(ImageCaptchaGeneratorProvider.class);
		if (!ArrayUtil.isEmpty(beanNamesForType)) {
			for (String beanName : beanNamesForType) {
				ImageCaptchaGeneratorProvider provider = beanFactory.getBean(beanName,
						ImageCaptchaGeneratorProvider.class);
				addImageCaptchaGeneratorProvider(provider);
			}
		}
	}

}
